package com.ys.web.pay;

import com.ys.base.BaseDao;
import com.ys.web.pay.model.*;
import com.ys.web.pay.model.vo.UserBank;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface PayMapper extends BaseDao {

    /**
     * 钱包开户
     *
     * @param wallet
     */
    @Insert("insert into user_wallet(uid,walletId,name,idCardNo,mobile,profession) values(#{uid},#{walletId},#{name},#{idCardNo},#{mobile},#{profession})")
    void walletCreate(Wallet wallet);

    /**
     * 钱包更新
     *
     * @param wallet
     */
    @Insert("update user_wallet set walletStatus=#{walletStatus},changeReason=#{changeReason},changeDateTime=#{changeDateTime} where walletId=#{walletId}")
    void walletEdit(Wallet wallet);

    /**
     * 钱包id查询
     *
     * @param uid
     * @return
     */
    @Select("select walletId from user_wallet where uid=#{uid}")
    String walletQuery(@Param("uid") long uid);
    @Select("select * from user_wallet where uid=#{uid}")
    Wallet wallet(@Param("uid") long uid);

    List<Map<String, Object>> getUserInfoByWalletIds(List<String> list);

    /**
     * 红包预下单
     *
     * @param redPacket
     */
    void redPacketCreate(RedPacket redPacket);

    /**
     * 根据交易流水号查询红包信息
     *
     * @param serialNumber
     * @return
     */
    RedPacket getRedPacketByNotify(String serialNumber);

    /**
     * 红包状态更新
     *
     * @param redPacket
     * @return
     */
    int redPacketUpdate(RedPacket redPacket);

    /**
     * 红包预下单
     *
     * @param redPacketDetail
     */
    void redPacketDetailCreate(List<RedPacketDetail> redPacketDetail);

    /**
     * @param id
     * @return
     */
    List<RedPacketDetail> getRedPacketDetail(@Param("id") long id);

    /**
     * 转账预下单
     *
     * @param transfer
     */
    void transferCreate(Transfer transfer);

    /**
     * 根据交易流水号查询转账信息
     *
     * @param serialNumber
     * @return
     */
    Transfer getTransferByNotify(String serialNumber);

    /**
     * 转账状态更新
     *
     * @param transfer
     * @return
     */
    int transferUpdate(Transfer transfer);

    /**
     * 转账预下单
     *
     * @param recharge
     */
    void rechargeCreate(Recharge recharge);

    /**
     * 根据交易流水号查询转账信息
     *
     * @param orderSn
     * @return
     */
    Recharge getRechargeByNotify(String orderSn);

    /**
     * 转账状态更新
     *
     * @param recharge
     * @return
     */
    int rechargeUpdate(Recharge recharge);

    /**
     * 充值成功
     *
     * @param recharge
     */
    void rechargeSucc(Recharge recharge);

    /**
     * 转账预下单
     *
     * @param withholding
     */
    void withholdingCreate(Withholding withholding);

    /**
     * 根据交易流水号查询转账信息
     *
     * @param orderSn
     * @return
     */
    Withholding getWithholdingByNotify(String orderSn);

    /**
     * 转账状态更新
     *
     * @param withholding
     * @return
     */
    int withholdingUpdate(Withholding withholding);

    List<RedPacket> getUnGrad(@Param("uid") long uid, @Param("groupId") int groupId);

    @Select("SELECT * from user_bank where card_token=#{bankId} and user_id=#{userId}")
    UserBank getUserBankByUserId(@Param("userId") Long userId, @Param("bankId") String bankId);
    @Select("SELECT count(0) from user_bank where user_id=#{userId}")
    int getUserBankByUserIdCount(@Param("userId") Long userId);


    @Select("SELECT * from user_bank_mb where card_token=#{bankId} and user_id=#{userId}")
    UserBank getUserBankByUserIdMb(@Param("userId") Long userId, @Param("bankId") String bankId);
    void payOrder(PayOrder payOrder);

    @Update("update pay_order set pay_status=#{payStatus},pay_time=now(),day_key=#{dayKey},serialNumber=#{serialNumber}" +
            " where id=#{id} and pay_status='0'")
    int payOrderStatusChange(PayOrder payOrder);
    @Update("update pay_order set pay_status=#{payStatus},pay_time=now(),day_key=#{dayKey}," +
            "serialNumber=#{serialNumber},card_no = #{cardNo}" +
            " where id=#{id} and pay_status='0'")
    int payOrderStatusZfb(PayOrder payOrder);
    @Update("update pay_order set pay_status=#{payStatus},orderRefundNo=#{orderRefundNo}" +
            " where id=#{id}")
    int payOrderRefund(PayOrder payOrder);

    @Select("select * from pay_order where order_no=#{orderNo}")
    PayOrder payOrderByNo(String orderNo);

    @Select("select * from pay_order where orderId=#{orderId}")
    PayOrder payOrderByOrderId(String orderId);

    @Select("select * from pay_order where serialNumber=#{serialNumber}")
    PayOrder payOrderBySerialNumber(String serialNumber);

    void initUserAccount(UserAccount userAccount);

    void initUserAccountZfb(UserAccount userAccount);

    @Select("select id,balance, freeze_amount as FreezeAmount from user_account where user_id=#{userId}")
    UserAccount getAccountByUserId(Long userId);

    @Select("select id,balance, freeze_amount as FreezeAmount from user_zfb_account where user_id=#{userId}")
    UserAccount getAccountZfbByUserId(Long userId);
    @Select("select id,balance, freeze_amount as FreezeAmount from user_hjzf_account where user_id=#{userId}")
    UserAccount getAccountHjzgByUserId(Long userId);


    void userAccountFlow(UserAccountFlow userAccountFlow);

    void userAccountZfbFlow(UserAccountFlow userAccountFlow);

    //@Update("update user_account set balance=#{balance},freeze_amount=#{freezeAmount},raw_update_time=now() where id=#{id} and balance=#{oldBalance} and freeze_amount=#{oldFreezeAmount} and balance+#{payAmount}>0")
    @Update("update user_account set balance=#{balance},freeze_amount=#{freezeAmount},raw_update_time=now() where id=#{id} and balance=#{oldBalance} and freeze_amount=#{oldFreezeAmount}")
    int userAccountUpdate(UserAccount userAccount);

    @Select("select * from pay_order where pay_status=0 and raw_add_time > date_add(NOW(),interval -60 minute) and raw_add_time < date_add(NOW(),interval -1 minute)")
    List<PayOrder> getPayOrderListByTime();

    @Update("update withdraw_record_wait set withdraw_status = 2 where id = #{id} and withdraw_status = 1")
    int updateStatus(@Param("id") String id);

    @Update("update user_zfb_account set balance=#{balance},freeze_amount=#{freezeAmount},raw_update_time=now() where id=#{id} and balance=#{oldBalance} and freeze_amount=#{oldFreezeAmount}")
    int userZfbAccountUpdate(UserAccount account);

    @Select("select id,balance, freeze_amount as FreezeAmount from user_hjzf_account where user_id=#{userId}")
    UserAccount getAccountHjzfByUserId(long userId);

    void initUserAccountHjzf(UserAccount userAccount);

    @Update("update user_hjzf_account set balance=#{balance},freeze_amount=#{freezeAmount},raw_update_time=now() where id=#{id} and balance=#{oldBalance} and freeze_amount=#{oldFreezeAmount}")
    int userHjzfAccountUpdate(UserAccount account);

    @Select("SELECT a.`user_id` userId,a.`mobile` mobile,a.`card_no` cardNo,a.`id_card_num` idCardNum," +
            "a.`user_name` userName,a.`bank_code` bankCode,a.`card_token` cardToken,b.bank_name bankName," +
            "a.`linkId` from user_bank_hjzf a left join s_sys_bank_mb b on a.bank_code = b.bank_code where card_token=#{bankId} and user_id=#{userId}")
    UserBank getUserBankByUserIdHjzf(@Param("userId")long userId, @Param("bankId")String bankId);

    @Select("SELECT a.`user_id` userId,a.`mobile` mobile,a.`card_no` cardNo,a.`id_card_num` idCardNum," +
            "a.`user_name` userName,a.`bank_code` bankCode,a.`card_token` cardToken,b.bank_name bankName," +
            "a.`linkId` from user_bank_hjzf a left join s_sys_bank_mb b on a.bank_code = b.bank_code where card_token=#{bankId} and user_id=#{userId}")
    UserBank getUserBankHjzfByUserId(@Param("userId")Long userId, @Param("bankId")String bankId);
}
